package cn.ztydata.teachingsystem.heaven.service;

import cn.ztydata.teachingsystem.exception.ServiceException;
import cn.ztydata.teachingsystem.heaven.cons.Roles;
import cn.ztydata.teachingsystem.heaven.dao.*;
import cn.ztydata.teachingsystem.heaven.entity.Department;
import cn.ztydata.teachingsystem.heaven.entity.Role;
import cn.ztydata.teachingsystem.heaven.entity.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 授权服务类
 *
 * @author fanruiping
 * @since 2014-12-02
 */
@Service
public class AuthorizeService extends BaseService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private CourseDao courseDao;
    @Autowired
    private TeacherTeamDao teacherTeamDao;
    @Autowired
    private SpecialtyDao specialtyDao;
    @Autowired
    private DepartmentDao departmentDao;
    @Autowired
    private DepartmentService departmentService;

    /**
     * 获取已授权用户列表
     * @param role 角色
     * @param deptId 系部编号
     * @param specialtyId 专业编号
     * @return List
     *
     * @author fanruiping
     * @since 2014-12-02
     */
    public List<Teacher> getAuthorisedList(int role, Long deptId, Long specialtyId){
        List<Role> entities = roleDao.getAuthorisedList(role, deptId, specialtyId);

        List<Long> teacherIds = new ArrayList<>();

        for(Role entity : entities){
            log.debug("循环授权用户列表");

            teacherIds.add(entity.getTeacherId());
        }

        List<Teacher> teachers = new ArrayList<>();

        //教师编号集合不为空
        if(!teacherIds.isEmpty()){
            log.debug("已授权用户编号集合不为空:!teacherIds.isEmpty()");

            //获取教师列表
            teachers = teacherDao.getByIds(teacherIds);

            List<String> deptNums = new ArrayList<>();

            for(Teacher entity : teachers){
                log.debug("循环教师列表:获取系部编码");

                deptNums.add(entity.getDeptNum());
            }

            //系部代码集合不为空
            if(!deptNums.isEmpty()){
                log.debug("教师所属系部编码集合不为空:!deptNums.isEmpty()");

                Map<String,String> departments = departmentDao.getNamesByNumbers(deptNums);

                log.debug("循环教师列表:填充系部名称");
                for(Teacher entity : teachers){
                    entity.setDeptName(departments.get(entity.getDeptNum()));
                }
            }
        }

        log.debug("返回授权用户列表");

        return teachers;
    }

    /**
     * 新增授权
     * @param entity 实体类
     *
     * @author fanruiping
     * @since 2014-12-02
     */
    @Transactional
    public void add(Role entity){
        //判断新增授权是否成功
        if(!roleDao.add(entity)){
            log.debug("add authorize fail");

            log.error("新增授权失败");

            throw new ServiceException("授权失败");
        }

        if(entity.getSpecialtyId() != 0 && !specialtyDao.updateLeader(entity.getSpecialtyId(),entity.getTeacherId())){
            log.debug("update specialty leader fail");

            log.error("更新专业负责人失败");

            throw new ServiceException("授权失败");
        }
    }

    /**
     * 验证是否已授权
     * @param entity 实体类
     * @return Boolean
     *
     * @author fanruiping
     * @since 2014-12-02
     */
    public Boolean checkIsExist(Role entity){
        log.debug("check authorize is exist");

        return roleDao.checkIsExist(entity);
    }

    /**
     * 删除指定授权
     * @param teacherId
     * @param roleId
     * @param deptId
     * @param specialtyId
     * @return int
     *
     * @author fanruiping
     * @since 2014-12-02
     */
    @Transactional
    public void delete(Long teacherId, int roleId, Long deptId, Long specialtyId){
        if(1 > roleDao.delete(teacherId, roleId, deptId,specialtyId)){
            log.debug("delete authorize fail");

            log.error("删除授权失败");

            throw new ServiceException("取消授权失败");
        }

        if(0 != specialtyId && !specialtyDao.updateLeader(specialtyId,Long.parseLong("0"))){
            log.debug("update specialty leader fail");

            log.error("修改专业负责人失败");

            throw new ServiceException("取消授权失败");
        }
    }

    /**
     * 更新课程负责人
     *
     * @author chenxin
     * @since 2014/12/24
     */
    @Transactional
    public void updateCourseLeader(final long creatorId, final long courseId) {
        //获取教师明细
        Teacher teacher = teacherDao.getById(creatorId);

        if(teacher == null){
            log.debug("this creator is not exist");

            log.warn("创建人不存在:creatorId->" + creatorId + ",courseId->" + courseId);
            throw new ServiceException();
        }

        //更新课程中的创建者(编号和名称)
        if (!courseDao.updateCreator(creatorId, teacher.getName(), courseId)) {
            log.debug("update lesson creator fail");

            log.warn("更新课程创建人失败:creatorId->" + creatorId + ",courseId->" + courseId);
            throw new ServiceException();
        }

        //删除指定课程负责人在课程教师团队中的记录
        teacherTeamDao.deleteExcludeLeader(courseId, creatorId);

        //更新教师团队中的课程负责人
        if (!teacherTeamDao.updateLeader(courseId, creatorId)) {
            log.debug("update lesson leader fail");

            log.warn("更新教师团队-课程负责人失败:courseId->" + courseId + ",creatorId->" + creatorId);
            throw new ServiceException();
        }
    }

    /**
     * 验证用户角色
     * @param teacherId 用户编号
     * @param roleId 角色编号
     * @return Boolean
     *
     * @author fanruiping
     * @since 2014-12-29
     */
    public Boolean checkRole(Long teacherId,int roleId){
        return roleDao.checkRole(teacherId,roleId);
    }
}
